<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Spatie\Permission\Models\Role;

class PHPExcelController extends Controller
{
    public function phpOiifce(){
        // 实例化类
        $spreadsheet = new Spreadsheet();
        // 操作第一个工作表
        $sheet = $spreadsheet->setActiveSheetIndex(0);
        //设置表名称
        $sheet->setTitle('测试列表');

        // 设置表格宽度
        $sheet->getColumnDimension('D')->setWidth(18);

        // 列名赋值
        $sheet->setCellValue('A1', 'ID');
        $sheet->setCellValue('B1', '角色名称');
        $sheet->setCellValue('C1', '英文标识');
        $sheet->setCellValue('D1', '创建时间');
        // 获取数据
        $data = Role::all();
        foreach($data as $k => $v){
            $i = 1;
            $sheet->setCellValue('A' .($i + 1), $data[$k]['id']);
            $sheet->setCellValue('B' .($i + 1), $data[$k]['title']);
            $sheet->setCellValue('C' .($i + 1), $data[$k]['name']);
            $sheet->setCellValue('D' .($i + 1), $data[$k]['created_at']);
        }
        $writer = new Xlsx($spreadsheet);
        $writer->save('hello world.xlsx');
        $url = 'hello world.xlsx';
        return $url;
    }

    public function phpOiifcer(){

    }
}
